package pingan.com.imandtwo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.activity.chat.Bean;
import pingan.com.imandtwo.activity.chat.RecyclerAdpateraa;
import pingan.com.imandtwo.utils.BaseActivity;

public class ChatActivity extends BaseActivity {


    @BindView(R.id.btn_send)
    TextView btnSend;
    @BindView(R.id.et_content_chat)
    EditText etContentChat;
    @BindView(R.id.liao_name)
    TextView liaoName;
    //    @BindView(R.id.et_name_chat)
//    EditText etNameChat;
    private MyEMMessageListener ml;

    private static final String TAG = "ChatActivity";
    private List<Bean> list = new ArrayList<>();

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 1:
                    Bean userBean = new Bean("", msg.obj + "", "1");
                    list.add(userBean);

                    recyclerAdpater.notifyDataSetChanged();
                    break;
            }
        }
    };
    private RecyclerAdpateraa recyclerAdpater;
    private RecyclerView recy;
    private String name;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_chat);
        ButterKnife.bind(this);

        recy = (RecyclerView) findViewById(R.id.tv);
        recyclerAdpater = new RecyclerAdpateraa(list, ChatActivity.this);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recy.setLayoutManager(manager);
        recy.setAdapter(recyclerAdpater);

        name = getIntent().getStringExtra("name");
        String tex = getIntent().getStringExtra("tex");
        String s = tex.toString();
        liaoName.setText(s);

        //发送消息
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建消息对象：参数：消息内容，接收人>>>此处注意顺序，容易出错导致接收不到信息
                EMMessage txtSendMessage = EMMessage.createTxtSendMessage(etContentChat.getText().toString(),
                        name.toString().trim());
                //发送消息
                EMClient.getInstance().chatManager().sendMessage(txtSendMessage);

                Bean userBean = new Bean("", etContentChat.getText().toString(), "0");
                list.add(userBean);
                recyclerAdpater.notifyDataSetChanged();

                etContentChat.setText("");
            }
        });

        ml = new MyEMMessageListener();
        EMClient.getInstance().chatManager().addMessageListener(ml);

    }

    /*
    * 消息监听
    */
    class MyEMMessageListener implements EMMessageListener {

        @Override
        public void onMessageReceived(List<EMMessage> list) {//此方法子线程中执行

            //解析消息
            for (EMMessage node : list) {
                final String from = node.getFrom();//发送者
                final EMMessage.Type type = node.getType();//消息类型

                switch (type) {
                    case TXT://文本内容
                        EMTextMessageBody body = (EMTextMessageBody) node.getBody();
                        final String message1 = body.getMessage();

                        //将修改内容交给主线程来更新UI
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Message message = new Message();
                                message.what = 1;
                                message.obj = message1;
                                handler.sendMessage(message);

                            }
                        });

                        break;
                    default:
                        break;
                }
            }
        }

        @Override
        public void onCmdMessageReceived(List<EMMessage> list) {

        }

        @Override
        public void onMessageRead(List<EMMessage> list) {

        }

        @Override
        public void onMessageDelivered(List<EMMessage> list) {

        }

        @Override
        public void onMessageRecalled(List<EMMessage> list) {

        }

        @Override
        public void onMessageChanged(EMMessage emMessage, Object o) {

        }
    }

    //取消监听避免内存泄漏
    @Override
    protected void onDestroy() {
        super.onDestroy();

        EMClient.getInstance().chatManager().removeMessageListener(ml);
    }
}
